import {createSlice, Slice} from "@reduxjs/toolkit";
import {RootState} from "../store";

//AntD的国际化 分辨是 中文 还是英文

interface AppString{
    lang : string
}

//获取localStorage当中存储的语言类型
function getInitialState<AppString>() {
    const lang = localStorage.getItem("lang") || "zh_CN";
    return {
        lang
    }
}
const initialState:AppString = getInitialState();

//切片
const appSlice:Slice = createSlice({
    //状态的名称
    name : "app",
    //初始化的数据
    initialState,
    //自定义一个reducer
    reducers : {
        setLang(state,{type,payload}){
            //en_US
            localStorage.setItem("lang",payload);
            //rtk immer : 对象发生改变,则全部深复制
            state.lang = payload;
        }
    }
});

//用来获取数据的方法
export const selectLang = (state : RootState) => state.app.lang;

//actions
export const {setLang} = appSlice.actions;

export default appSlice.reducer;